﻿using MODEL;
using SysCommon.WinServices;
using SysController;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace AmazonMWS
{
    partial class ProductsServices : ServiceBase, IServiceController
    {
        public ProductsServices()
        {
            InitializeComponent();
        }

        public void Start()
        {
            SysCommon.Log.Log.Write("Product Services Statr...");
        }

        #region 2015-11-18 竞争信息 - void GetCompetitivePricingForASIN_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        /// <summary>
        /// 2015-11-18 竞争信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetCompetitivePricingForASIN_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var item in SystemController.RunAccount)
            {
                List<SellerListing> lists = SystemController.GetSellerListing(item.AccountInfoID);
                if (lists == null || lists.Count < 0)
                    continue;
                List<string> list = lists.Select(s => s.ASIN).Distinct().ToList(); 
                int rPage = (int)Math.Ceiling(list.Count / 20.0);

                for (int i = 1; i <= rPage; i++)
                {
                    List<CompetitiveModel> listCompetitiveModel = ProductsController.GetCompetitivePricingForASIN(item, list.Skip((i - 1) * 20).Take(20).ToList());
                    if (listCompetitiveModel.Count < 0)
                        continue;
                    SysCommon.Log.Log.Write(string.Format("获取竞争信息：{0} 条", listCompetitiveModel.Count));
                    foreach (var ls in listCompetitiveModel)
                    {
                        if (ls.IsSetCompetitivePriceModelList)
                        {
                            int row = SystemController.AddCompetitivePrice(ls.CompetitivePriceModelList);
                            SysCommon.Log.Log.Write(string.Format("修改CompetivtivePrice {0} 条", row));
                        }
                        if (ls.IsSetSalesRenkModel)
                        {
                            int row = SystemController.AddSalesRank(ls.SalesRenkModel);
                            SysCommon.Log.Log.Write(string.Format("修改SalesRan {0} 条", row)); 
                        }
                    } 
                }
            }
        }
        #endregion

         
    }
}
